package view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.View;

public class SlidingFrame2D extends SlidingFrame {

    Camera camera;
    Matrix matrix;
    private int height;
    private int width;

    public SlidingFrame2D(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        init();
    }

    private void init() {
        camera = new Camera();
        matrix = new Matrix();
    }


    public SlidingFrame2D(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        init();
    }

    @SuppressLint("NewApi")
    @Override
    public void layoutChildren() {
        leftContiner.layout(0, 0, width, height);
        centreContiner.layout(position, 0, width + position, height);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        leftContiner.measure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    @Deprecated
    public void setLeftView(View v) {
    }

    @Override
    @Deprecated
    public void setRightView(View v) {
    }

    public void setBackgroupView(View v) {
        super.setLeftView(v);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        width = w;
        height = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        float scale = 1f * position / width;
        drawChild(canvas, leftContiner, 0);
        camera.save();
        camera.rotateY(-15 * scale);
        camera.getMatrix(matrix);
        camera.restore();
        canvas.scale(1f - scale * 0.6f, 1f - scale * 0.6f, width - position, height / 2);
        canvas.translate(0, height / 2);
        canvas.concat(matrix);
        canvas.translate(0, -height / 2);
        drawChild(canvas, centreContiner, 0);
    }
}
